package com.ruoyi.web.controller.kyhs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.kysh.entity.AcceptInf;
import com.ruoyi.system.kysh.service.AcceptInfAddressService;
import com.ruoyi.system.kysh.service.AcceptInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/kyhs/accept-inf")
public class AcceptInfController {
    @Autowired
    private AcceptInfAddressService acceptInfAddressService;

    @Autowired
    private AcceptInfService acceptInfService;

    @PostMapping("/save-accept-inf")
    public AjaxResult saveAcceptInf(@RequestBody AcceptInf acceptInf, @RequestParam Long taskId) {
        if (acceptInf.getLaborCount() == null || acceptInf.getClassFootage() == null
                || acceptInf.getGroutingCondition().equals("") || acceptInf.getSeeRock().equals("")) {
            return AjaxResult.warn("查看信息是否选择完整");
        }

        boolean b = acceptInfService.saveAcceptInf(acceptInf, taskId);
        if(b){
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.warn("该工单已验收");

    }

    @PostMapping("/check-accept-inf")
    public AjaxResult checkAcceptInf(@RequestBody AcceptInf acceptInf) {

        if (acceptInf.getAcceptCount() == null || acceptInf.getVideoCondition().equals("")) {
            return AjaxResult.warn("未选择完整");
        }
        acceptInfService.checkAndUpdateAcceptInf(acceptInf);
        return AjaxResult.success("保存成功");
    }

    @PostMapping("/check-accept-inf/export")
    public void export() {

    }

    @GetMapping("/select-accpet-inf")
    public AjaxResult selectAcceptInf (@RequestParam("acceptNum") String acceptNum){
        if (acceptNum == null || "".equals(acceptNum)) {
            return AjaxResult.warn("请输入相应信息");
        }
        //查询
        String json = acceptInfService.selectAcceptIdByAcceptNum(acceptNum);
        return AjaxResult.success("操作成功", json);
    }
    @PostMapping("/bind-accpet-inf")
    public AjaxResult bindvideoaddress (@RequestParam("bindVideo") String bindVideo, @RequestParam("acceptId") Long acceptId){
        if (acceptId == null || bindVideo == null || "".equals(bindVideo)) {
            return AjaxResult.warn("请输入相应信息");
        }
        //更新视频路径
        int result = acceptInfService.bindvideoaddress(bindVideo, acceptId);
        if (result == 1)
            return AjaxResult.success("绑定成功");
        return AjaxResult.error();
    }
    @PostMapping("/bind1-accpet-inf")
    public AjaxResult bindvideoaddress1 (@RequestBody AcceptInf acceptInf){
        if (acceptInf.getBindVideo() == null || StringUtils.isBlank(acceptInf.getBindVideo())) {
            return AjaxResult.error("请输入相应信息");
        }
        LambdaQueryWrapper<AcceptInf> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(acceptInf.getAcceptId() != null, AcceptInf::getAcceptId, acceptInf.getAcceptId());
        AcceptInf acceptInf1 = acceptInfAddressService.getOne(queryWrapper);
        if ((acceptInf1.getBindVideo() != null) && (acceptInf1.getBindVideo().equals(acceptInf.getBindVideo()))) {
            return AjaxResult.error("该视频地址已绑定");
        }
        LambdaQueryWrapper<AcceptInf> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(AcceptInf::getAcceptId, acceptInf.getAcceptId());
        AcceptInf acceptInf2 = new AcceptInf();
        acceptInf2.setBindVideo(acceptInf.getBindVideo());
        boolean update = acceptInfAddressService.update(acceptInf2, queryWrapper1);
        if (update) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error();
        }
    }
}
